﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.Text.RegularExpressions;
using EntityClassGenerator.EntityClassGen;
using EntityClassGenerator.UserSetting;
using EntityClassGenerator.DataAccess;
using System.Runtime.InteropServices;
using System.Diagnostics;


namespace EntityClassGenerator.UI
{
    /**说明:
     * ===============================================================
     * 1. 代码过多难于维护;
     *    解决方法:将代码分门别类地分散到多个局部类中; 
     *             使用#region ...#endregion将代码分割成多个部分.
     *             在实际项目中应尽量避免这种超大的类.
     * 
     * 2. 命名不太规范, 难于阅读维护.
     *    解决方法:各种控件,变量的命名严格遵循规范;
     *             在项目开发之初,定义系统的特有命名规范,此规范前提是遵循标准规范.
     * 
     * 3. 应加强对私有方法的抽取,以提高代码重用性,减少代码量.
     *    解决方法:对重用性较高的逻辑单元抽取为私有方法.
     * 
     * 4. 在使用不完全可靠的对象之前,应做必要的验证,以提高系统的稳定性.
     *================================================================*/


    /// <summary>
    /// 本类实体类生成器主窗体.
    /// </summary>
    public partial class EntityClassGenerator : Form
    {     
        //包含手动添加的字段信息.
        private System.Data.DataTable  _fieldsInfo;

        //标志系统设置是否改变.
        private bool _settingsChanged = false;

        //包含从数据库中得到的所有的类信息及相关的类注释的信息.
        private Hashtable _clasInfo = new Hashtable();
        private Hashtable _clasRemarkInfo = new Hashtable();

        //标志应该保存的字段注释的位置,以便于定位保存.
        private int _index;
        private string _tabName2;
        private bool _saveFieldRemark;

        //标志应该保存的类注释的位置,以便于定位保存.
        private string _tabName;
        private bool _saveClassRemark;

        /// <summary>
        /// 构造方法.
        /// </summary>
        public EntityClassGenerator()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EntityClassGenerator_Load(object sender, EventArgs e)
        {
            //控制数据库参数输入控件的外观.
            this.gpbSqlServer.Visible = true;
            this.gpbMSAccess.Visible = false;
            this.gpbOracle.Visible = false;
            this.lblIsGenerating.Visible = false;
            this.gpbMSAccess.Location = new Point(32,69);
            this.gpbOracle.Location = new Point(32,69);
            this.gpbSqlServer.Location = new Point(32,69);

            //加载用户设置信息.
            UserSettings.LoadUserSettings();
            //初始化用户设置控件.
            this.InitUserSettingControl();
            this.ApplySettingsWarning();  
            
            //使初始状态显示第三个选项卡.
            //this.tbcMain.SelectedIndex = 2;

            //初始化第二个选项卡的数据类型下拉框.
            if (this.rdbDotNet.Checked == true)
            {
                this.InitDotNetTypesList(); 
            }
            else 
            {
                this.InitJavaTypesList();
            }         
            this.cmbType.SelectedIndex = 0;

            //初始化Access数据驱动程序选择下拉框.
            this.cmbAccessProvider.SelectedIndex = 0;

            //初始化第二个选项卡的字段信息表格.
            this.InitFieldsTable();
        }

        /// <summary>
        /// '添加字段按钮'的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddField_Click(object sender, EventArgs e)
        {
            //得到字段名.
            string f_name = this.txtField.Text.Trim();

            //字段名的非空验证.
            if (f_name.Equals(""))
            {
                MessageBox.Show("字段名不能为空！"
                                , "警告"
                                , MessageBoxButtons.OK
                                , MessageBoxIcon.Warning);
                this.txtField.Focus();
                return;
            }

          
            //判断用户输入的字段名是否格式正确.字段名必须仅包含字母,数字,下划线.
            if(!Regex.IsMatch(f_name,@"^\w+$",RegexOptions.Singleline))
            {
                 MessageBox.Show("您输入的字段名有误!"
                                , "警告"
                                , MessageBoxButtons.OK
                                , MessageBoxIcon.Warning);
                this.txtField.Focus();
                return;               
            }
            
            //判断是否已存在用户输入的字段名.
            if (this.ExistNewField(f_name) == true)
            {
                MessageBox.Show("所指定字段已存在！"
                                , "警告"
                                , MessageBoxButtons.OK
                                , MessageBoxIcon.Warning);
                this.txtField.Focus();
                return;
            }

            //得到数据类型,注释.
            string f_type = this.cmbType.Text;
            string f_remark = this.txtRemark.Text.Trim();

            //向字段信息表中添加数据.
            this._fieldsInfo.Rows.Add(new object[] { f_name,f_type,f_remark});
            this.dgvFields.DataSource = this._fieldsInfo;            

            //初始化字段信息输入框.
            this.InitFieldInputControl();
        } 

        /// <summary>
        /// 第二个选项卡的'生成实体类'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerator_Click(object sender, EventArgs e)
        {
            //得到用户输入的类相关信息.
            string namspace = this.txtNamespace.Text.Trim();
            string claName = this.txtClass.Text.Trim();
            string claRemark = this.txtClassRemark.Text.Trim();   

            //命名空间名和类名的非空验证.
            if (namspace.Equals("") || claName.Equals(""))
            {
                MessageBox.Show("命名空间名和类名不能为空！"
                        , "警告"
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Warning);
                this.txtNamespace.Focus();
                return;
            }

            //判断是否已存在字段信息.
            if (this._fieldsInfo.Rows.Count == 0)
            {
                MessageBox.Show("须先添加字段才能产生实体类！"
                        , "警告"
                        , MessageBoxButtons.OK
                        , MessageBoxIcon.Warning);
                this.txtField.Focus();
                return;
            }      
            
            //生成实体类.
            if (UserSettings.Language.Equals("Java"))
            {                
                //为了尽可能早地释放占用的资源而使用using语法．
                using(JavaEntityClassGen java = 
                                new JavaEntityClassGen(namspace, 
                                                        UserSettings.References, 
                                                        claName, 
                                                        claRemark, 
                                                        (DataTable)this.dgvFields.DataSource))
                {
                    if (java.Save(UserSettings.SavePath, 
                        GeneratorTool.CapFirstLetter(claName) + UserSettings.Postfix + ".Java"))
                    {
                        MessageBox.Show("成功生成!");
                        //初始化第二个选项卡的相关输入控件.
                        this.InitTabControlOfFromManual();
                    }
                    else
                    {
                        MessageBox.Show("生成失败!");
                    }
                }                      
            }
            else
            {
                using( DotNetEntityClassGen dotNet = 
                                new DotNetEntityClassGen(namspace, 
                                                        UserSettings.References, 
                                                        claName, 
                                                        claRemark, 
                                                        (DataTable)this.dgvFields.DataSource))
                {
                    if(dotNet.Save(UserSettings.SavePath,
                        GeneratorTool.CapFirstLetter(claName)+UserSettings.Postfix+".cs"))
                    {
                        MessageBox.Show("成功生成!");
                        this.InitTabControlOfFromManual();
                    }
                    else
                    {
                        MessageBox.Show("成功失败!");
                    }
                }         
            }
        }

        /// <summary>
        /// '清除表格数据'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.InitFieldsTable();
        } 

        /// <summary>
        /// '字段输入框'的键盘按下事件处理.
        /// 若按下的是空格，则使＇类型选择下拉框'获得焦点.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtField_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                this.cmbType.Focus();
            }
        }

        /// <summary>
        /// '类型选择下拉框'的键盘按下事件处理.
        /// 若按下的是空格，则使＇注释输入框'获得焦点.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbType_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                this.txtRemark.Focus();
            }
        }

        /// <summary>
        /// 单选按钮'SQL Server'的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdbSqlServer_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdbSqlServer.Checked != true)
            {
                return;
            }
            this.gpbSqlServer.Visible = true;
            this.gpbMSAccess.Visible = false;
            this.gpbOracle.Visible = false;
        }

        /// <summary>
        /// 单选按钮'MS Access'的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdbMSAccess_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdbMSAccess.Checked != true)
            {
                return;
            }
            this.gpbSqlServer.Visible = false;
            this.gpbMSAccess.Visible = true;
            this.gpbOracle.Visible = false;
        }

        /// <summary>
        /// 单选按钮'Oracle'的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radOracle_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rdbOracle.Checked != true)
            {
                return;
            }
            this.gpbSqlServer.Visible = false;
            this.gpbMSAccess.Visible = false;
            this.gpbOracle.Visible = true;
        }

        /// <summary>
        ///  单选按钮'.Net实体类'的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdbDotNet_CheckedChanged(object sender, EventArgs e)
        {
            //标志设置已改变.
            this.SettingsChangedWarning();

            if (this.rdbDotNet.Checked != true)
            {
                return;
            }
            this.txtDotNetPostfix.Enabled = true;
            this.lblDotNetPostfix.Enabled = true;
            this.txtJavaPostfix.Enabled = false;
            this.lblJavaPostfix.Enabled = false;
        }

        /// <summary>
        ///  单选按钮'Java实体类'的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdbJava_CheckedChanged(object sender, EventArgs e)
        {
            this.SettingsChangedWarning();
            if (this.rdbJava.Checked != true)
            {
                return;
            }
            this.txtDotNetPostfix.Enabled = false;
            this.txtJavaPostfix.Enabled = true;
            this.lblJavaPostfix.Enabled = true;
            this.lblDotNetPostfix.Enabled = false;
        }

        /// <summary>
        /// 保存路径'浏览'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result=this.fbdSavePath.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.txtSavePath.Text = this.fbdSavePath.SelectedPath;
            }
        }

        /// <summary>
        /// ＇添加＇菜单项事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiAdd_Click(object sender, EventArgs e)
        {           
            string dbType = null;
            DataTable data = null;
            if (this.tbcDataTypeMapping.SelectedTab.Tag.Equals("SqlServer"))
            {
                dbType = "SqlServer";
                data = UserSettings.SqlDataMapping;
            }
            else if (this.tbcDataTypeMapping.SelectedTab.Tag.Equals("Access"))
            {
                dbType = "Access";
                data = UserSettings.AccessDataMapping;
            }
            else 
            {
                dbType = "Oracle";
                data = UserSettings.OracleDataMapping;
            }

            EditDataTypeMapping add = new EditDataTypeMapping(dbType);
            add.Text = "添加数据映射信息";
            add._useTo = "add";
            add._source = data;
            add.ShowDialog();
            this.SettingsChangedWarning();
        }

        /// <summary>
        /// ＇修改＇菜单项事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiChange_Click(object sender, EventArgs e)
        {
            string dbType = null;
            DataTable data = null;
            DataGridView view = null;
            if (this.tbcDataTypeMapping.SelectedTab.Tag.Equals("SqlServer"))
            {
                dbType = "SqlServer";
                data = UserSettings.SqlDataMapping;
                view = this.dgvSqlMapping;
            }
            else if (this.tbcDataTypeMapping.SelectedTab.Tag.Equals("Access"))
            {
                dbType = "Access";
                data = UserSettings.AccessDataMapping;
                view = this.dgvAccessMapping;
            }
            else
            {
                dbType = "Oracle";
                data = UserSettings.OracleDataMapping;
                view = this.dgvOracleMapping;
            }

            EditDataTypeMapping add = new EditDataTypeMapping(dbType);
            add.Text = "添加数据映射信息";
            add._useTo = "change";
            add._source = data;
            add._rowIndex = view.CurrentRow.Index;
            add.ShowDialog();
            this.SettingsChangedWarning();
        }

        /// <summary>
        /// ＇删除＇菜单项事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiDelete_Click(object sender, EventArgs e)
        {          
            DataTable data = null;
            DataGridView view = null;
            if (this.tbcDataTypeMapping.SelectedTab.Tag.Equals("SqlServer"))
            {               
                data = UserSettings.SqlDataMapping;
                view = this.dgvSqlMapping;
            }
            else if (this.tbcDataTypeMapping.SelectedTab.Tag.Equals("Access"))
            {               
                data = UserSettings.AccessDataMapping;
                view = this.dgvAccessMapping;
            }
            else
            {               
                data = UserSettings.OracleDataMapping;
                view = this.dgvOracleMapping;
            }

            if (data.Rows.Count == 0)
            {
                return;
            }

            int index = view.CurrentRow.Index;
            data.Rows.RemoveAt(index);
            this.SettingsChangedWarning();
        }

        /// <summary>
        /// 主选项卡的选择改变事件．
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbcMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            //标签文本的改变.
            if (this.rdbDotNet.Checked == true)
            {
                this.lblNamespace1.Text = "命名空间名:";
                this.lblNamespace2.Text = "命名空间名:";
            }
            else 
            {
                this.lblNamespace1.Text = "      包名:";
                this.lblNamespace2.Text = "      包名:";
            }

            //其它改变.
            if (this.tbcMain.SelectedTab.Tag.Equals("FromDB"))
            {
                ;
            }
            else if (this.tbcMain.SelectedTab.Tag.Equals("Manual"))
            {
                if (UserSettings.Language.Equals("Java"))
                {
                    this.InitJavaTypesList();
                }
                else
                {
                    this.InitDotNetTypesList();
                }
            }
            else 
            {
                ;
            }
        }

        /// <summary>
        /// 当'.Net后缀','Java后缀','制表符大小'输入框的文本改变时的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtDotNetPostfix_TextChanged(object sender, EventArgs e)
        {
            this.SettingsChangedWarning();
        }

        /// <summary>
        /// 当'引用','存储路径'输入框的文本改变时的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rtbReferencesList_TextChanged_1(object sender, EventArgs e)
        {
            this.SettingsChangedWarning();
        }

        /// <summary>
        /// '应用设置'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApplySetting_Click(object sender, EventArgs e)
        {
            /*
             * * 改变内存中的用户设置信息
             * **/
            if (this.rdbDotNet.Checked == true)
            {
                UserSettings.Language = ".Net";
                UserSettings.Postfix = this.txtDotNetPostfix.Text.Trim();
            }
            else 
            {
                UserSettings.Language = "Java";
                UserSettings.Postfix = this.txtJavaPostfix.Text.Trim();
            }
            UserSettings.TabSize = Convert.ToInt32(this.txtTabSize.Text.Trim());
            UserSettings.SavePath = this.txtSavePath.Text.Trim();
            UserSettings.References = UserSettings.FormatStringArray(
                    Regex.Split(this.rtbReferencesList.Text.Trim(), "\n+", RegexOptions.None));
            UserSettings.SqlDataMapping = (DataTable)this.dgvSqlMapping.DataSource;
            UserSettings.AccessDataMapping = (DataTable)this.dgvAccessMapping.DataSource;
            UserSettings.OracleDataMapping = (DataTable)this.dgvOracleMapping.DataSource;

            /*
             * 保存用户设置信息
             * ***/
            if (UserSettings.SaveUserSetting())
            {
                MessageBox.Show("用户设置信息保存成功!", "保存信息");
                this.ApplySettingsWarning();                
            }
            else 
            {
                MessageBox.Show("用户设置信息保存失败!", "保存信息");
                //重新加载用户设置信息到内存.
                UserSettings.LoadUserSettings();
            }            
        }

        /// <summary>
        /// 'Sql的集成安全验证'复选框的选择改变事件.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckbIntegratedSecurity.Checked == true)
            {
                this.lblSqlPwd.Enabled = false;
                this.lblSqlUserid.Enabled = false;
                this.txtUserName.Enabled = false;
                this.txtPassword.Enabled = false;
            }
            else 
            {
                this.lblSqlPwd.Enabled = true;
                this.lblSqlUserid.Enabled = true;
                this.txtUserName.Enabled = true;
                this.txtPassword.Enabled = true;
            }
        }

        /// <summary>
        ///  'Access的集成安全验证'复选框的选择改变事件.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckbAccessIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckbAccessIntegratedSecurity.Checked == true)
            {
                this.lblAccessPwd.Enabled = false;
                this.lblAccessUserName.Enabled = false;
                this.txtAccessPassword.Enabled = false;
                this.txtAccessUserID.Enabled = false;
            }
            else
            {
                this.lblAccessPwd.Enabled = true;
                this.lblAccessUserName.Enabled = true;
                this.txtAccessPassword.Enabled = true;
                this.txtAccessUserID.Enabled = true;
            }
        }

        /// <summary>
        /// 选择Access数据库文件'浏览'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAccessBrowser_Click(object sender, EventArgs e)
        {
            if (this.ofdSelectAccessDbFile.ShowDialog() == DialogResult.OK)
            {
                this.txtAccessDataSource.Text = this.ofdSelectAccessDbFile.FileName;
            }
        }

        /// <summary>
        /// SqlServer'连接数据库'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnectDatabase_Click(object sender, EventArgs e)
        {
            //得到数据库连接参数.
            string server = this.txtServer.Text.Trim();
            bool security = this.ckbIntegratedSecurity.Checked;
            string uid = this.txtUserName.Text.Trim();
            string pwd = this.txtPassword.Text.Trim();

            //相关验证.
            if (server.Equals(""))
            {
                MessageBox.Show("服务器名不能为空!","警告"
                                ,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            if (security == false && uid.Equals(""))
            {
                MessageBox.Show("用户名不能为空!", "警告"
                                , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //得到数据库连接字符串.
            string conStr = AccessSqlServer.GetConnectionStr(server, security, uid, pwd);

            //访问数据库.
            AccessSqlServer.OpenConnection(conStr);
            DataTable dbs = AccessSqlServer.GetAllDbName();
            AccessSqlServer.CloseConnection();

            //显示数据库服务器中的所有的数据库名.
            this.cmbDatabaseName.DataSource = dbs;
            this.cmbDatabaseName.DisplayMember = "name";

            this.lblSqlDatabaseName.Enabled = true;
            this.cmbDatabaseName.Enabled = true;
            this.btnShowTables.Enabled = true;
        }

        /// <summary>
        /// Access'连接数据库'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAccessConnect_Click(object sender, EventArgs e)
        {
            //得到数据库参数.
            string provider = this.cmbAccessProvider.Text.Trim();
            bool security = this.ckbAccessIntegratedSecurity.Checked;
            string uid = this.txtAccessUserID.Text.Trim();
            string pwd = this.txtAccessPassword.Text.Trim();
            string dataSource = this.txtAccessDataSource.Text.Trim();

            //相关验证.
            if (dataSource.Equals(""))
            {
                MessageBox.Show("请指定Access数据库!", "警告"
                               , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (security == false && uid.Equals(""))
            {
                MessageBox.Show("用户名不能为空!", "警告"
                               , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            //得到数据库连接字符串.
            string conStr = AccessMSAccess.GetConnectionStr(provider, security, 
                                                            uid, pwd,dataSource);

            //访问数据库,并得到数据库中的所有的表和视图信息.
            AccessMSAccess.OpenConnection(conStr);
            DataTable tabs = AccessMSAccess.GetAllTableAndViewName();
            AccessMSAccess.CloseConnection();            

            //显示数据库中的所有的表和视图信息.
            if (tabs != null)
            {
                this.clbTablesAndView.Items.Clear();
                for (int i = 0; i < tabs.Rows.Count; i++)
                {
                    this.clbTablesAndView.Items.Add(tabs.Rows[i][0],true);
                }                
            }

            //得到所有的表和视图的结构信息.
            this.GetMSAccessTablesStructure(tabs);

            this.lblTabList.Text = "MS Access表/视图:";
        } 

        /// <summary>
        /// Oracle'连接数据库'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOracleConnectDb_Click(object sender, EventArgs e)
        {
            //得到数据库参数.
            string server = this.txtOracleServer.Text.Trim();
            string uid = this.txtOracleUserID.Text.Trim();
            string pwd = this.txtOraclePwd.Text.Trim();

            //相关验证.
            if (server.Equals("")||uid.Equals(""))
            {
                MessageBox.Show("服务名和用户名均不能为空!"
                                , "警告"
                                , MessageBoxButtons.OK
                                , MessageBoxIcon.Warning);
                return;
            }            

            //得到连接字符串.
            string conStr = AccessOracle.GetConnectionStr(server,uid,pwd);

            //访问数据库.
            AccessOracle.OpenConnection(conStr);
            DataTable tabs = AccessOracle.GetAllTableAndViewName();
            AccessOracle.CloseConnection();

            //必要的验证.
            if (tabs == null)
            {
                return;
            }

            //得到所有表和视图的结构信息.
            this.GetOracleTablesStructure(tabs);

            //显示所有的表和视图的名称.
            if (tabs != null)
            {
                this.clbTablesAndView.Items.Clear();
                for (int i = 0; i < tabs.Rows.Count; i++)
                {
                    string type = tabs.Rows[i][1].ToString().Trim().ToUpper();
                    string temp = null;
                    if (type.Equals("VIEW"))
                    {
                        temp = "@ ";
                    }
                    else
                    {
                        temp = "";
                    }
                    this.clbTablesAndView.Items
                        .Add(temp+ tabs.Rows[i][0].ToString(),true);
                }
            }

            //显示第一个表或视图的结构信息.
            if (this.clbTablesAndView.Items.Count > 0)
            {
                this.clbTablesAndView.SelectedIndex = 0;
                string tabName = this.GetTableOrViewName
                                (this.clbTablesAndView.Items[0].ToString());
                this.lblTabStructure.Text = "表" + tabName + "相关实体类结构:";
                this.FormatDgvTableStructure();
            }
            this.lblTabList.Text = "Oracle表/视图:";
          
        }

        /// <summary>
        /// '显示用户表'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShowTables_Click(object sender, EventArgs e)
        {
            //得到用户所选择的数据库名.
            string dbName = this.cmbDatabaseName.Text;

            //访问数据库.
            AccessSqlServer.OpenConnection();
            DataTable tabs = AccessSqlServer.GetAllTableAndViewName(dbName);
            AccessSqlServer.CloseConnection();

            //显示所有的表和视图的名称.
            if (tabs != null)
            {
                this.clbTablesAndView.Items.Clear();
                for (int i = 0; i < tabs.Rows.Count; i++)
                {
                    string type = tabs.Rows[i][1].ToString().Trim();
                    string temp = null;
                    if (type.Equals("V"))
                    {
                        temp = "@ ";
                    }
                    else 
                    {
                        temp = "";
                    }
                    this.clbTablesAndView.Items.Add(temp+tabs.Rows[i][0].ToString(),true);
                }
            }

            //得到所有的表和视图的结构信息.
            this.GetSqlServerTablesStructure(this.cmbDatabaseName.Text.Trim(),tabs);

            //显示第一个表或视图的结构信息.
            if (this.clbTablesAndView.Items.Count > 0)
            {
                this.clbTablesAndView.SelectedIndex = 0;
                string tabName = this.GetTableOrViewName
                            (this.clbTablesAndView.Items[0].ToString());
                this.lblTabStructure.Text = "表" + tabName + "相关实体类结构:";
                DataTable data=(DataTable)this._clasInfo[tabName];
                this.dgvTableOrViewStructure.DataSource = data;                           
                this.FormatDgvTableStructure();                
            }
            
            this.lblTabList.Text = "SqlServer表/视图:";
            this.chbSelectAll.Checked = true;
        }    

        /// <summary>
        /// '删除选定字段'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteField_Click(object sender, EventArgs e)
        {
            if (this._fieldsInfo.Rows.Count == 0)
            {
                return;
            }
            int index = this.dgvFields.CurrentRow.Index;
            this._fieldsInfo.Rows.RemoveAt(index);            
        }

        /// <summary>
        /// '修改选定字段'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditField_Click(object sender, EventArgs e)
        {
            if (this._fieldsInfo.Rows.Count == 0)
            {
                return;
            }

            //得到选定字段的信息.
            int index = this.dgvFields.CurrentRow.Index;
            string fieName=this._fieldsInfo.Rows[index][0].ToString();
            string fieRemark = this._fieldsInfo.Rows[index][2].ToString();

            EditFieldInfo editField = new EditFieldInfo();
            editField.txtFieldName.Text = fieName;
            editField.txtFieldRemark.Text = fieRemark;        
            editField.cmbFieldType.DataSource = this.cmbType.DataSource;
            editField._source = this._fieldsInfo;
            editField._index = this.dgvFields.CurrentRow.Index;
            editField.ShowDialog();
        }

        /// <summary>
        /// '表和视图列表框'的选择改变事件．
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clbTablesAndView_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name=this.clbTablesAndView
                       .Items[this.clbTablesAndView.SelectedIndex].ToString();
            string tabName = this.GetTableOrViewName(name);
            if (name.StartsWith("@"))
            {
                this.lblTabStructure.Text = "视图" + tabName + "相关实体类结构:";
            }
            else
            {
                this.lblTabStructure.Text = "表" + tabName + "相关实体类结构:";
            }
            DataTable data=(DataTable)this._clasInfo[tabName];
            this.dgvTableOrViewStructure.DataSource = data;

            //决定是否保存类注释.
            if (this._saveClassRemark)
            {
                this._clasRemarkInfo[this._tabName] = this.txtTabControlOneClassRemark.Text;
                this._saveClassRemark = false;
            }

            //决定是否保存字段注释.
            if (this._saveFieldRemark)
            {
                DataTable data2=(DataTable)this._clasInfo[this._tabName2];
                data2.Rows[this._index][2] = this.txtTabControlOneFieldInfo.Text;
                this._saveFieldRemark = false;
            }

            this.txtTabControlOneClassRemark.Text=this._clasRemarkInfo[tabName].ToString();
            int index = this.dgvTableOrViewStructure.CurrentRow.Index;
            if(data.Rows.Count>0)
            {
                this.txtTabControlOneFieldInfo.Text=data.Rows[index][2].ToString();
            }      
      
            //更改类注释标签文本.
            string className = EntityClassGen.GeneratorTool.CapFirstLetter(tabName) 
                             + UserSetting .UserSettings.Postfix;
            this.lblClassRemark.Text = className + "类注释:";
        }

        /// <summary>
        /// '表和视图结构显示＇控件的单击事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvTableOrViewStructure_Click(object sender, EventArgs e)
        {
            if (this.clbTablesAndView.SelectedIndex < 0)
            {
                return;
            }
            string tabName = this.GetTableOrViewName
                  (this.clbTablesAndView.Items[this.clbTablesAndView.SelectedIndex].ToString());
            DataTable data = (DataTable)this._clasInfo[tabName];
            int index = this.dgvTableOrViewStructure.CurrentRow.Index; 
           
            if (this._saveFieldRemark)
            {                
                data.Rows[this._index][2]=this.txtTabControlOneFieldInfo.Text;
                this._saveFieldRemark = false;
            }
            this.txtTabControlOneFieldInfo.Text=data.Rows[index][2].ToString();

            //更改字段注释文本.

        }

        /// <summary>
        /// '字段注释输入框'获得焦点的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTabControlOneFieldInfo_Enter(object sender, EventArgs e)
        {
            if (this.dgvTableOrViewStructure.CurrentRow == null)
            {
                return;
            }

            if (this.clbTablesAndView.SelectedIndex < 0)
            {
                return;
            }

            //标示将要保存字段注释信息的位置.
            this._index = this.dgvTableOrViewStructure.CurrentRow.Index;
            this._tabName2 = this.GetTableOrViewName
               (this.clbTablesAndView.Items[this.clbTablesAndView.SelectedIndex].ToString());
            this._saveFieldRemark = true;
        }

        /// <summary>
        /// '类注释输入框'获得焦点的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTabControlOneClassRemark_Enter(object sender, EventArgs e)
        {
            if (this.clbTablesAndView.SelectedIndex < 0)
            {
                return;
            }
            //标示将要保存类注释信息的位置.
            this._tabName = this.GetTableOrViewName
                (this.clbTablesAndView.Items[this.clbTablesAndView.SelectedIndex].ToString());
            this._saveClassRemark = true;
        }

        /// <summary>
        /// '从数据库生成实体类'按钮的事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGeneratedFromDb_Click(object sender, EventArgs e)
        {
            //相关验证.
            if (this.txtTabControlOneNamespace.Text.Trim().Equals(""))
            {
                MessageBox.Show("命名空间名/包名不能为空!");
                return;
            }
            if (this._clasInfo.Count <= 0)
            {
                MessageBox.Show("请先选择数据库表或视图!");
                return;
            }

            //得到用户选择的所有的表或视图名.
            CheckedListBox.CheckedItemCollection checkedItems = 
                                                 this.clbTablesAndView.CheckedItems;

            //得到用户输入的命名空间名.
            string namespaceName = this.txtTabControlOneNamespace.Text.Trim();
           
            //初始化进度条.
            this.pbGeneratorProgress.Minimum = 0;
            this.pbGeneratorProgress.Maximum = checkedItems.Count;
            this.pbGeneratorProgress.Step = 1;
            this.pbGeneratorProgress.Value = 0;
            this.lblIsGenerating.Visible = true; ;
            this.pbGeneratorProgress.Visible = true;

            //生成实体类.
            foreach (object item in checkedItems)
            {
                string tabName= this.GetTableOrViewName(item.ToString());
               
                if (UserSettings.Language.Equals("Java"))
                {
                    //为了尽可能早地释放占用的资源而使用using语法．
                    using (JavaEntityClassGen java = 
                            new JavaEntityClassGen(namespaceName, 
                                                    UserSettings.References, 
                                                    tabName, 
                                                    this._clasRemarkInfo[tabName].ToString(), 
                                                    (DataTable)this._clasInfo[tabName]))
                    {
                        java.Save(UserSettings.SavePath, 
                            GeneratorTool.CapFirstLetter(tabName) + UserSettings.Postfix + ".Java") ;              
                    }                    
                }
                else
                {
                    using (DotNetEntityClassGen dotNet = 
                            new DotNetEntityClassGen(namespaceName, 
                                                    UserSettings.References, 
                                                    tabName, 
                                                    this._clasRemarkInfo[tabName].ToString(), 
                                                    (DataTable)this._clasInfo[tabName]))
                    {
                        dotNet.Save(UserSettings.SavePath, 
                            GeneratorTool.CapFirstLetter(tabName) + UserSettings.Postfix + ".cs");                    
                    }
                }
                this.pbGeneratorProgress.Increment(1);
            }

            this.lblIsGenerating.Visible = false;
            this.pbGeneratorProgress.Visible = false;
            MessageBox.Show("实体类已全部生成成功!");

            //打开生成的实体类文件所在的文件夹.
            Process.Start(UserSetting.UserSettings.SavePath);
        }

        /// <summary>
        /// 初始化手动添加字段信息的表格.
        /// </summary>
        private void InitFieldsTable()
        {
            //得到表结构.
            this._fieldsInfo = new DataTable("FieldsTable");
            this._fieldsInfo.Columns.Add("字段名", typeof(string));
            this._fieldsInfo.Columns.Add("字段类型", typeof(string));
            this._fieldsInfo.Columns.Add("注释", typeof(string));

            //将对象表绑定到表格控件上,并控制显示样式.
            this.dgvFields.DataSource = this._fieldsInfo;
            this.dgvFields.Columns[2].Width = 300;
            this.dgvFields.RowHeadersWidth = 27;
        }

        /// <summary>
        /// 将第二个选项卡的数据类型下拉框,初始化DotNet类型列表．
        /// </summary>
        private void InitDotNetTypesList()
        {
            string[] types = new string[] { "bool", "int", "long", "char", 
                                            "string", "float", "Datetime", 
                                            "double", "byte", "decimal", "short" };
            this.cmbType.DataSource = types;
        }

        /// <summary>
        /// 将第二个选项卡的数据类型下拉框, 初始化Java类型列表．
        /// </summary>
        private void InitJavaTypesList()
        {
            string[] types = new string[] { "boolean", "int", "long", "char", 
                                            "String", "byte", "short", "float", "double" };
            this.cmbType.DataSource = types;
        }

        /// <summary>
        /// 当手动添加字段名时,验证新的字段名是否已存在．
        /// </summary>
        /// <param name="newField">新字段名</param>
        /// <returns>是否存在（ture/false）</returns>
        private bool ExistNewField(string newField)
        {
            for (int i = 0; i < this._fieldsInfo.Rows.Count; i++)
            {
                DataRow row = this._fieldsInfo.Rows[i];
                if (row[0].ToString().Equals(newField))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 初始化第二个选项卡的相关输入控件.
        /// </summary>
        private void InitTabControlOfFromManual()
        {
            this.txtClass.Text = "";
            this.txtClassRemark.Text = "";
            //初始化第二个选项卡的字段信息表格.
            this.InitFieldsTable();
        }

        /// <summary>
        /// 初始化第二个选项卡的字段输入控件．
        /// </summary>
        private void InitFieldInputControl()
        {
            this.txtField.Text = "";
            this.cmbType.SelectedIndex = 0;
            this.txtRemark.Text = "";
            this.txtField.Focus();
        }

        /// <summary>
        /// 将字段信息添加到数据表格中.
        /// </summary>
        /// <param name="f_name">字段名</param>
        /// <param name="f_type">字段类型</param>
        /// <param name="f_remark">字段注释</param>
        private void AddFieldInfo(string f_name, string f_type, string f_remark)
        {
            this._fieldsInfo.Rows.Add(new object[] { f_name, f_type, f_remark });
        }


        /// <summary>
        /// 本方法用于初始化用户设置的相关控件内容.
        /// </summary>
        private void InitUserSettingControl()
        {
            //数据的显示.
            if (UserSettings.Language.Equals("Java"))
            {
                this.rdbJava.Checked = true;
                this.txtJavaPostfix.Text = UserSettings.Postfix;
                this.lblNamespace1.Text = "      包名:";
                this.lblNamespace2.Text = "      包名:";
            }
            else
            {
                this.rdbDotNet.Checked = true;
                this.txtDotNetPostfix.Text = UserSettings.Postfix;
                this.lblNamespace1.Text = "命名空间名:";
                this.lblNamespace2.Text = "命名空间名:";
            }

            this.txtTabSize.Text = UserSettings.TabSize.ToString();

            for (int i = 0; i < UserSettings.References.Length; i++)
            {
                this.rtbReferencesList.Text += UserSettings.References[i] + "\n";
            }

            this.txtSavePath.Text = UserSettings.SavePath;

            this.dgvAccessMapping.DataSource = UserSettings.AccessDataMapping;
            this.dgvSqlMapping.DataSource = UserSettings.SqlDataMapping;
            this.dgvOracleMapping.DataSource = UserSettings.OracleDataMapping;

            //控件样式的设置.
            this.dgvAccessMapping.Columns[0].Width = 145;
            this.dgvAccessMapping.Columns[1].Width = 150;
            this.dgvAccessMapping.RowHeadersWidth = 30;
            this.dgvAccessMapping.ColumnHeadersHeight = 30;

            this.dgvSqlMapping.Columns[0].Width = 145;
            this.dgvSqlMapping.Columns[1].Width = 150;
            this.dgvSqlMapping.RowHeadersWidth = 30;
            this.dgvSqlMapping.ColumnHeadersHeight = 30;

            this.dgvOracleMapping.Columns[0].Width = 145;
            this.dgvOracleMapping.Columns[1].Width = 150;
            this.dgvOracleMapping.RowHeadersWidth = 30;
            this.dgvOracleMapping.ColumnHeadersHeight = 30;
        }

        /// <summary>
        /// 当设置改变时,应该执行的代码.
        /// </summary>
        private void SettingsChangedWarning()
        {
            this._settingsChanged = true;
            this.btnApplySetting.ForeColor = Color.Red;
        }

        /// <summary>
        /// 当成功应用设置时,应该执行的代码.
        /// </summary>
        private void ApplySettingsWarning()
        {
            this._settingsChanged = false;
            this.btnApplySetting.ForeColor = Color.Black;
        }

        /// <summary>
        /// 得到MSAccess数据库中所有表的结构信息.
        /// </summary>
        /// <param name="dbName">数据库名称</param>
        /// <param name="tabs">数据库中的表信息</param>
        private void GetMSAccessTablesStructure(DataTable tabs)
        {
            this._clasInfo.Clear();
            this._clasRemarkInfo.Clear();
            string tabName = null;
            DataTable tabStructure = null;
            for (int i = 0; i < tabs.Rows.Count; i++)
            {
                tabName = tabs.Rows[i][0].ToString();
                AccessMSAccess.OpenConnection();
                tabStructure = AccessMSAccess.ConvertTableOrViewStructure
                               (AccessMSAccess.GetTableOrViewStructure( tabName));
                AccessMSAccess.CloseConnection();
                this._clasInfo.Add(tabName, tabStructure);
                this._clasRemarkInfo.Add(tabName, "");
            }
        }


        /// <summary>
        /// 得到SqlServer数据库中所有表的结构信息.
        /// </summary>
        /// <param name="dbName">数据库名称</param>
        /// <param name="tabs">数据库中的表信息</param>
        private void GetSqlServerTablesStructure(string dbName, DataTable tabs)
        {
            this._clasInfo.Clear();
            this._clasRemarkInfo.Clear();
            string tabName = null;
            DataTable tabStructure = null;
            for (int i = 0; i < tabs.Rows.Count; i++)
            {
                tabName = tabs.Rows[i][0].ToString();
                AccessSqlServer.OpenConnection();
                tabStructure = AccessSqlServer.ConvertTableOrViewStructure
                               (AccessSqlServer.GetTableOrViewStructure(dbName, tabName));
                AccessSqlServer.CloseConnection();
                this._clasInfo.Add(tabName, tabStructure);
                this._clasRemarkInfo.Add(tabName, "");
            }
        }

        /// <summary>
        /// 得到Oracle数据库中所有表的结构信息.
        /// </summary>        
        /// <param name="tabs">数据库中的表信息</param>
        private void GetOracleTablesStructure(DataTable tabs)
        {
            this._clasInfo.Clear();
            this._clasRemarkInfo.Clear();
            string tabName = null;
            DataTable tabStructure = null;
            for (int i = 0; i < tabs.Rows.Count; i++)
            {
                tabName = tabs.Rows[i][0].ToString();
                AccessOracle.OpenConnection();
                tabStructure = AccessOracle.ConvertTableOrViewStructure
                               (AccessOracle.GetTableOrViewStructure(tabName));
                AccessOracle.CloseConnection();
                this._clasInfo.Add(tabName, tabStructure);
                this._clasRemarkInfo.Add(tabName, "");
            }
        }

        /// <summary>
        /// 要据列表项控件中的表或视图的名称,得到真实的表或视图的名称．
        /// </summary>
        /// <param name="source">列表项控件中的名称</param>
        /// <returns>实际名称</returns>
        private string GetTableOrViewName(string source)
        {
            if (source.StartsWith("@"))
            {
                return source.Substring(2);
            }
            else
            {
                return source;
            }
        }

        /// <summary>
        /// 控制第一个选项卡中'表结构信息显示控件'的外观.
        /// </summary>
        private void FormatDgvTableStructure()
        {
            this.dgvTableOrViewStructure.Columns[0].Width = 135;
            this.dgvTableOrViewStructure.Columns[1].Width = 138;
        }

        /// <summary>
        /// '全选'复选框的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chbSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chbSelectAll.Checked)
            {
                SelectOrUnselectAllTablesAndViews(true);
            }
            else
            {
                SelectOrUnselectAllTablesAndViews(false);
            }
        }

        /// <summary>
        /// 全选(true)或全不选(false)所有的表格和视图.
        /// </summary>
        /// <param name="select">选择状态</param>
        private void SelectOrUnselectAllTablesAndViews(bool select)
        {
            //得到列表中的所有的项.
            CheckedListBox.ObjectCollection collection = this.clbTablesAndView.Items;

            //将列表项内容缓存到数组中.
            string[] items = new string[collection.Count];
            int i = 0;
            foreach(Object item in collection)
            {
                items[i] = item.ToString();
                i++;
            }

            //清除列表中的内容.
            this.clbTablesAndView.Items.Clear();
           
            //将缓存的内容以指定的选中状态加载到列表中.
            foreach (Object item in items)
            {               
                this.clbTablesAndView.Items.Add(item, select);
            }

            //默认选中第一项.
            if (this.clbTablesAndView.Items.Count > 0)
            {
                this.clbTablesAndView.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// '表和视图结构'表格的选择改变事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvTableOrViewStructure_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow currentRow = this.dgvTableOrViewStructure.CurrentRow;
            if (currentRow == null)
            {
                return;
            }
            string fieldName = currentRow.Cells[0].Value.ToString();
            this.lblFieldRemark.Text = fieldName + "字段注释:";
        }

        /// <summary>
        /// 字段注释输入框的'Ctrl+上下键头(↑↓)'按下事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTabControlOneFieldInfo_KeyUp(object sender, KeyEventArgs e)
        {
            //必要的验证.
            if (this.dgvTableOrViewStructure.Rows == null ||
                this.dgvTableOrViewStructure.Rows.Count < 1 ||
                this.dgvTableOrViewStructure.CurrentRow == null)
            {
                return;
            }

            //得到当前选择行索引.
            int index = this.dgvTableOrViewStructure.CurrentRow.Index;
            if (index < 0)
            {
                return;
            }

            //得到当前选择行的上一行.
            int upIndex = 0;
            if (index > 0)
            {
                upIndex = index - 1;
            }

            //得到当前选择行的下一行.            
            int downIndex = this.dgvTableOrViewStructure.Rows.Count - 1;
            if (index != downIndex)
            {
                downIndex = index + 1;
            }

            //表示新行的矩形区域.
            Rectangle row = new Rectangle();

            //若用户按下Ctrl + ↑.
            if (e.Control == true && e.KeyCode == Keys.Up)
            {
                row = this.dgvTableOrViewStructure.GetRowDisplayRectangle(upIndex, true);
            }

            //若用户按下Ctrl + ↓.
            if (e.Control == true && e.KeyCode == Keys.Down)
            {
                row = this.dgvTableOrViewStructure.GetRowDisplayRectangle(downIndex, true);          
            }


            //得到鼠标的点击位置.
            int x = this.Left + this.Padding.Left +
                    this.tbcMain.Left + this.tbcMain.Margin.Left +
                    this.dgvTableOrViewStructure.Left + this.dgvTableOrViewStructure.Margin.Left + 
                    row.Left + 10;

            int y = this.Top + this.Padding.Top +
                    this.tbcMain.Top + this.tbcMain.Margin.Top +
                    this.dgvTableOrViewStructure.Top + this.dgvTableOrViewStructure.Margin.Top + 
                    (row.Top + row.Bottom) / 2 + 35;          

            //设置鼠标位置.
            WinAPIMethods.SetCursorPos(x, y);

            //鼠标左键按下.
            WinAPIMethods.mouse_event(WinAPIMethods.MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            //鼠标左键释放.
            WinAPIMethods.mouse_event(WinAPIMethods.MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);

            //模拟鼠标点击字段注释输入框.
            int xx = this.Left + this.Padding.Left +
                     this.tbcMain.Left + this.tbcMain.Margin.Left +
                     this.txtTabControlOneFieldInfo.Left + this.txtTabControlOneFieldInfo.Margin.Left + 
                     30;
            int yy = this.Top + this.Padding.Top +
                     this.tbcMain.Top + this.tbcMain.Margin.Top +
                     this.txtTabControlOneFieldInfo.Top + this.txtTabControlOneFieldInfo.Margin.Top + 
                     60;

            //设置鼠标位置.
            WinAPIMethods.SetCursorPos(xx , yy);

            //鼠标左键按下.
            WinAPIMethods.mouse_event(WinAPIMethods.MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
            //鼠标左键释放.
            WinAPIMethods.mouse_event(WinAPIMethods.MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);

            //保存字段注释.
            this.txtTabControlOneFieldInfo_Enter(new object(), new EventArgs());

        }
  
        /// <summary>
        /// 类注释输入框的'Ctrl+上下键头(↑↓)'按下事件处理.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtTabControlOneClassRemark_KeyUp(object sender, KeyEventArgs e)
        {
            //必要的验证.
            if (this.clbTablesAndView.Items.Count <= 0)
            {
                return;
            }

            //得到当前选择项索引.
            int index = this.clbTablesAndView.SelectedIndex;
            if (index < 0)
            {
                return;
            }

            //得到当前选择项的上一项.
            int upIndex = 0;
            if (index > 0)
            {
                upIndex = index - 1;
            }

            //得到当前选择项的下一项.            
            int downIndex = this.clbTablesAndView.Items.Count - 1;
            if (index != downIndex)
            {
                downIndex = index + 1;
            }

            //若用户按下Ctrl + ↑.
            if (e.Control == true && e.KeyCode == Keys.Up)
            {                
                this.clbTablesAndView.SelectedIndex = upIndex;
                //执行类注释输入框的获取焦点逻辑,以便于保存注释信息.
                this.txtTabControlOneClassRemark_Enter(new object(), new EventArgs());                
                return;
            }

            //若用户按下Ctrl + ↓.
            if (e.Control == true && e.KeyCode == Keys.Down)
            {                
                this.clbTablesAndView.SelectedIndex = downIndex;
                //执行类注释输入框的获取焦点逻辑,以便于保存注释信息.
                this.txtTabControlOneClassRemark_Enter(new object(), new EventArgs());
                return;
            }
        }
    }
}